<?php

namespace App\Admin\Controllers;

use App\Admin\Tools;
use App\Models\Image;
use App\Models\Product;
use App\Models\ProductSku;
use Encore\Admin\Controllers\AdminController as Controller;
use App\Models\ProductSkuAttribute;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Support\Facades\DB;

class ProductSkuController extends Controller
{
    protected $title = "商品SKU管理";

    protected function grid()
    {
        $grid = new Grid(new ProductSku);

        $grid->model()->latest();
        $grid->filter(function ($filter) {
            $filter->like('product.name', '商品名');
        });
        $grid->id('SKU_ID');
        $grid->product()->name('商品名');
        $grid->attr('SKU描述')->display(function ($attrs) {

            $attrs = array_map(function ($attr) {
                return "<span class='label label-success'>{$attr['attribute_value']}</span>";
            }, $attrs);

            return join('&nbsp;', $attrs);
        });
        $grid->price('价格')->display(function ($prices) {
            return Tools::price($prices);
        });
        $grid->stock('库存');
        $grid->created_at('添加时间');
        $grid->updated_at('更新时间');

        $grid->actions(function ($actions) {
            $actions->disableView();
        });

        return $grid;
    }

    protected function form()
    {
        $form = new Form(new ProductSku);
        $form->select('product_id', '所属商品')->options(function ($id) {
            $product = Product::query()->find($id);
            if ($product) {
                return [$product->id => $product->name];
            }
        })->ajax(route('admin.api.products'))
            ->load('attr', route('admin.api.attributes'));

        $form->multipleSelect('attr', '属性')->options(function ($val) {
            if (!$val) {
                $product = Product::query()->latest()->first();
            } else {
                $product = ProductSkuAttribute::query()
                    ->whereIn('id', $val)->first(['product_id']);
            }

            if ($product) {
                return ProductSkuAttribute::query()
                    ->whereProductId($product->product_id)->get()
                    ->pluck('attribute_value', 'id');
            }
        });

        $form->image('cover.path', '封面');
        $form->hidden('cover.type', '图片类型')
            ->value(Image::SKU_COVER);
        $form->text('price.current', '现价');
        $form->text('price.original', '原价');
        $form->text('price.wholesale', '批发价格');
        $form->number('stock', '库存');

        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();

            // 去掉`删除`按钮
            $tools->disableDelete();

            // 去掉`查看`按钮
            $tools->disableView();
        });

        return $form;
    }

    public function store()
    {
        $data = request()->all([
            'product_id',
            'attr',
            'price',
            'stock',
            'cover',
        ]);

        foreach ($data['attr'] as $k => $val) {
            if (is_null($val)) {
                unset($data['attr'][$k]);
            }
        }

        DB::transaction(function () use ($data) {
            $sku = new ProductSku();
            $sku->product_id = $data['product_id'];
            $sku->price = $data['price'];
            $sku->stock = $data['stock'];
            $sku->save();
            $this->form()->update($sku->id);
        });
    }

    public function update($id)
    {
        $data = request()->all([
            'product_id',
            'attr',
            'price',
            'stock',
            'cover',
        ]);

        foreach ($data['attr'] as $k => $val) {
            if (is_null($val)) {
                unset($data['attr'][$k]);
            }
        }

        DB::transaction(function () use ($data, $id) {
            $sku = ProductSku::find($id);
            $sku->product_id = $data['product_id'];
            $sku->price = $data['price'];
            $sku->stock = $data['stock'];
            $sku->save();
            $this->form()->update($sku->id);
        });

        return redirect()->route('admin.skus.index');
    }
}
